﻿/*
 * by cansik / Florian
 * Datum: 29.04.2009
 * Zeit: 10:55
 */
using System;
using System.Collections.Generic;
using System.Text;
using ProviderHandler;


namespace sEmit.Core
{

    //sEmit Main Class
    public class SMSEmitter
    {
        //Hilfs Variabeln

        //Provider -> Schnittstelle für veraltete Programme
        public IWebSMSHandler wsmsh;

        //Files
        public string ConfigFile { get; set; }
        public string PluginFolder { get; set; }
        public bool IsLoggedIn { get; private set; }

        public IWebSMSHandler[] ProviderList
        {
            get
            {
                return LoadProviders(PluginFolder, "IWebSMSHandler");
            }
        }

        //Konstruktor
        public SMSEmitter()
        {

        }

        //Loggt sich in das System ein
        public bool Login(string Username, string Password)
        {
            wsmsh.LoginName = Username;
            wsmsh.Password = Password;

            if (wsmsh.Login())
            {
                IsLoggedIn = true;
                return true;
            }
            else
            {
                IsLoggedIn = false;
                return false;
            }
        }

        //Loggt sich erneut mit den selben userdaten an
        public bool ReLogin()
        {
            if (wsmsh.Login())
            {
                IsLoggedIn = true;
                return true;
            }
            else
            {
                IsLoggedIn = false;
                return false;
            }
        }

        //Sendet eine SMS
        public bool SendSMS(Contact recipient, string message)
        {
            return (wsmsh.SendSMS(recipient.Number, message));
        }

        //Listet alle Provider auf
        private IWebSMSHandler[] LoadProviders(string directory, string strInterface)
        {
            IWebSMSHandler[] wsmshProvider;
            PluginServices.AvailablePlugin[] providers = PluginServices.FindPlugins(directory, strInterface);
            
            if (providers != null)
            {
                Int32 counter = 0;
                wsmshProvider = new IWebSMSHandler[providers.Length];
                foreach (PluginServices.AvailablePlugin provider in providers)
                {
                    wsmshProvider[counter] = (IWebSMSHandler)PluginServices.CreateInstance(provider);
                    counter++;
                }
            }
            else
            {
                wsmshProvider = new IWebSMSHandler[0];
            }

            return wsmshProvider;
        }

        //Setzt den Provider
        public void SetProvider(IWebSMSHandler Provider)
        {
            wsmsh = Provider;
            wsmsh.Initialize();
        }

        //Gibt die Account Infos zurueck
        public AccountInformation GetInformation(){return wsmsh.GetInformation();}

        //Kontakt verwaltung
        public bool AddContact(Contact c) { return wsmsh.AddContact(c.Name, c.Number); }
        public bool DeleteContact(Contact c) { return wsmsh.DeleteContact(c.Name, c.Number); }
        public Contact[] GetContacts() { return wsmsh.GetContacts(); }
    }

}
